import { roleGet, userLogin } from "@/api/user";
import router from "@/router";
export default {
  namespaced: true,
  state() {
    return {
      userInfo: null, //存放用户信息
    };
  },
  mutations: {
    initInfoMut(state, info) {
      state.userInfo = info;
    },
    // 刷新后尝试提取本地存储的用户信息
    localInfoMut(state) {
      let info = localStorage.getItem("admin-2206-userinfo");
      if (info) {
        state.userInfo = JSON.parse(info);
      }
    },
    //退出登录
    logoutMut(state) {
      state.userInfo = null;
      localStorage.removeItem("admin-2206-userinfo");
    },
  },
  actions: {
    userLoginAction(context, account) {
      userLogin(account).then(async (res) => {
        // console.log(res);
        let roleInfo = await roleGet(res.data.roleid); //以角色id请求角色权限数据
        console.log(roleInfo);
        res.data.roleInfo = roleInfo.data; //在用户信息中追加角色权限信息
        context.commit("initInfoMut", res.data);
        localStorage.setItem("admin-2206-userinfo", JSON.stringify(res.data));
        router.push("/");
      });
    },
  },
};
